package com.jt9003.controller;

import com.jt9003.pojo.Drugs;
import com.jt9003.pojo.StockInquiry;
import com.jt9003.pojo.Warehouse;
import com.jt9003.service.ConfimReceiptService;
import com.jt9003.service.StockInquiryService;
import com.jt9003.utils.PageUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import java.lang.String;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/confirm")
//确认入库单
public class ConfimReceiptController {

    @Resource
    private ConfimReceiptService con;
    @Resource
    private StockInquiryService stockInquiryService;

    @ResponseBody
    @RequestMapping("/get")
    //分页显示,模糊查询根据药品类型和单据号查询未确认入库的入库单
    public  Object getWarehouseList(@RequestParam(value ="documentNo",required = false) String documentNo,
                                     @RequestParam(value ="page",required = false,defaultValue = "1") Integer page,
                                     @RequestParam(value ="limit",required = false,defaultValue = "5") Integer limit
                                    ){
        Map map=new HashMap();
        int start = PageUtils.getStart(page,limit);
        Map param=new HashMap();
        param.put("documentNo",documentNo);
        param.put("start",start);
        param.put("pageSize",limit);
    try{
        List<Warehouse> warList=con.getWarehouseList(param);
        Integer warCount= con.warCount(param);
            map.put("msg", "查询成功");
            map.put("success", true);
            map.put( "count",warCount);
            map.put("code", 0);
            map.put("data", warList);
        }catch (Exception e) {
        e.printStackTrace();
        map.put("msg", "查询失败");
        map.put("success", false);
    }
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    @Transactional
    //根据单据号删除入库单据
    public  Object deleteWarehouseById(String documentNo){
        Map<String,Object> map=new HashMap<String,Object>();
        try {
            Integer i = con.deleteWar(documentNo)+con.deleteDrugs(documentNo);
            if (i==2) {
                map.put("message", "删除成功");
                map.put("success", true);
            } else {
                map.put("message", "删除失败");
                map.put("success", false);
            }
        }catch (Exception e){
            map.put("message", "发生异常，删除失败");
            map.put("success", false);
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }

    @RequestMapping("/update")
    @ResponseBody
    @Transactional
    //修改入库单信息 入库单表 药品表
    public  Object updateWarehouse(Warehouse warehouse){
        Map<String,Object> map=new HashMap<String,Object>();
        //调用修改入库单表和药品表的方法
        try {
            Integer i = con.updateWarehouse(warehouse);
            if (i == 1) {
                map.put("message", "修改成功");
                map.put("success", true);
            } else {
                map.put("message", "修改失败");
                map.put("success", false);
            }
        }catch(Exception e){
            e.printStackTrace();
            map.put("message", "发生异常，修改失败");
            map.put("success", false);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }
    /*确认入库单页面
     *点击修改或者查看详情时，根据单据号连表查询入库单和药品信息
     * */
    @RequestMapping("/queryWarehouse")
    @ResponseBody
    public Object queryWarehouse(String documentNo){
        Map<String,Object> map=new HashMap<String,Object>();
        try{
            Warehouse warehouse =con.selectWarAndDrugs(documentNo);
            map.put("message", "查询成功");
            map.put("success", true);
            map.put("result", warehouse);
        }catch (Exception e){
            map.put("message","查询失败");
            map.put("success",false);
            map.put("result", null);
        }
        return map;
    }


    //确认入库单页面，确认入库，修改入库状态为已确认,同时更新库存信息
    @RequestMapping("/updateWarStatus")
    @ResponseBody
    @Transactional
    public Object updateWarStatus( @RequestParam(value="documentNo",required=false)  String documentNo,
                                   @RequestParam(value="drugsName",required=false)  String drugsName,
                                   @RequestParam(value ="drType",required = false) String drType,
                                   @RequestParam(value ="inventoryNumber",required = false) Integer inventoryNumber,
                                   @RequestParam(value ="warehouseUnit",required = false) String warehouseUnit) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("drugsName",drugsName);
        param.put("drType",drType);
        param.put("inventoryNumber",inventoryNumber);
        param.put("warehouseUnit",warehouseUnit);

        Map<String, Object> param2 = new HashMap<String, Object>();
        param2.put("drugsName",drugsName);
        try {
           StockInquiry stockInquiry =stockInquiryService.isDrugs(param);
            if(stockInquiry!=null){
                 Integer inventoryNumberSum =stockInquiry.getInventoryNumber()+inventoryNumber;

                 System.out.print("要更新的药品数量"+inventoryNumberSum+"======================");

                 param2.put("inventoryNumber",inventoryNumberSum);
                 Integer result=stockInquiryService.updateInventory(param2);
                 if(result>0){
                     map.put("message1", "库存存在药品名,更新数据成功");
                     map.put("success1", true);
                 }else{
                     map.put("message1", "更新库存数据失败");
                     map.put("success1", false);
                 }
            }else{
               Integer result = stockInquiryService.addInventory(param);
                if(result>0){
                    map.put("message2", "库存不存在药品名,插入数据成功");
                    map.put("success2", true);
                }else{
                    map.put("message2", "更新库存数据失败");
                    map.put("success2", false);
                }
            }

            int i = con.updateWarStrtus(documentNo);
            if(i>0){
                map.put("message", "修改入库状态成功,已入库");
                map.put("success", true);
            }else{
                map.put("message", "修改失败");
                map.put("success", false);
                System.out.println("修改状态失败");
            }
        }catch (Exception e) {
            map.put("message", "发生异常，修改入库状态失败");
            map.put("success", false);
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }
}
